﻿using Scorpicore.Data;
using Scorpicore.Models;
using Scorpicore.MVC.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace Scorpicore.MVC.Models
{
    public class CheckBanknoteViewModel
    {
        public string SerialLetters { get; set; }
        public string SerialNumber { get; set; }
        public decimal TotalDistance { get; set; }
        public List<Destination> Points { get; set; }
        public Denomination Denomination { get; set; }
        public string PointsAsJson { get; set; }

        private DataContext Context;

        public bool LoadBanknote(string letters, string serialNumber)
        {
            this.Context = new DataContext();
            bool hasBanknote = false;
            var result = this.Context.Banknotes.Where(banknote => banknote.Letters == letters &&
                banknote.SerialNumber == serialNumber);
            if (result.Any())
            {
                hasBanknote = true;
                Banknote checkedBanknote = result.FirstOrDefault() as Banknote;
                this.SerialLetters = checkedBanknote.Letters;
                this.SerialNumber = checkedBanknote.SerialNumber;
                this.TotalDistance = checkedBanknote.Distance;
                this.Denomination = checkedBanknote.Denomination;
                this.Points = this.GetBanknoteRoute(checkedBanknote.Id);
            }
            var serializer = new JavaScriptSerializer();
            this.PointsAsJson = serializer.Serialize(this.Points);

            return hasBanknote;
        }

        private List<Destination> GetBanknoteRoute(int id)
        {
            var destinations = (from point in this.Context.Tracks
                          where point.Banknote.Id == id
                          orderby point.Date
                          select new Destination()
                          {
                              EntryTime = point.Date,
                              Location = point.Place.Name,
                              Message = point.Message,
                              Point = new Point()
                              {
                                  Latitude = (double)point.Place.Latitude,
                                  Longitude = (double)point.Place.Longitude
                              }
                          }).ToList();

            double distance = 0d;

            for (int i = 1; i < destinations.Count; i++)
            {
                destinations[i].Distance = Calculations.DistanceBetweenPoints(destinations[i - 1].Point, destinations[i].Point);
                destinations[i].TravelTime = destinations[i].EntryTime - destinations[i - 1].EntryTime;
                distance += destinations[i].Distance;
            }

            var banknote = this.Context.Banknotes.FirstOrDefault(b => b.Id == id);
            banknote.Distance = Convert.ToDecimal(distance);
            this.Context.SaveChanges();

            return destinations;
        }
    }
}